package leetcode;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 描述:226. 翻转二叉树
 *
 * @author Madison You
 * @created 10:10
 */
public class Easy_226_InvertTree {
    public static void main(String[] args) {

    }

    public TreeNode invertTree(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        if (root != null) deque.push(root);
        while (!deque.isEmpty()) {
            TreeNode node = deque.peek();
            if (node != null) {
                deque.pop();
                if (node.left != null) deque.push(node.left);
                if (node.right != null) deque.push(node.right);
                deque.push(node);
                deque.push(null);
            } else {
                deque.pop();
                node = deque.peek();
                deque.pop();
                swap(node);
            }
        }
        return root;
    }

    private void swap(TreeNode root) {
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
    }
}
